package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.Room;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoomMapper {
    @Insert("INSERT INTO rms_room (`title`,`address`,`info`,`capacity`,`deleted`) " +
            "VALUES" +
            " (#{title},#{address},#{info},#{capacity},#{deleted})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Room room);

    @Select("select t.id, t.title,t.address, t.info, " +
            "t.capacity ,t.deleted, t.created, t.updated from rms_room t "+
            "where t.id = #{param1} and t.deleted = 0")
    Room selectById(Long id);

    @Update("<script>" +
            "update rms_room " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='address != null'> address = #{address}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='capacity != null'> capacity = #{capacity}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "</set> " +
            "where id = #{id} and deleted =0 " +
            "</script>")
    int updateById(Room room);

    @Update("<script>" +
            "update rms_room set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update rms_room set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Select("select t.id, t.title, t.address, t.info ," +
            "t.capacity, t.deleted from rms_room t "+
            "where t.deleted = 0")
    List<Room> list();

    @Select("select t.id, t.title, t.address, t.info ," +
            "t.capacity t.deleted from rms_room t "+
            "where t.deleted = 0")
    PageInfo<Room> page(PageDTO pageDTO);

    @Select("select t.id, t.title, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_room t " +
            "where t.title = #{title} ")
    Room selectByAllTitle(Room room);

    @Delete("delete from rms_room where id = #{param1}")
    int deleteById(Long id);

    @Select("select t.id, t.title, t.address, t.info, t.capacity, t.deleted, t.created, t.updated from rms_room t " +
            "where t.deleted = 0 and t.title like concat('%', #{param1}, '%')")
    List<Room> listLikeTitle(String title);
}
